Page({
  data: {
    orders: [], // 订单列表
    tableData: [],
    fromData: {
      amount: 0,
      balance: 0
    },
    dialogAddMallGoods: false,
    dialogAddMallAs: false,
    comment: {},
    orderId: ""
  },

  onLoad() {
    this.getOrders();
  },

  getOrders() {
    const userToken = wx.getStorageSync('token');  // 获取存储的 token
    wx.request({
      url: 'http://localhost:8001/goods/goodsOrderApp/orderList', // 替换为实际API
      method: 'POST',
      header: {
        'Token': userToken,
      },
      success: (res) => {
        console.log(res);
        if (res.data.code === 20000) {
          this.setData({
            orders: res.data.data.dataList
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error(err);
      }
    });
  },

  uptOrder(e) {
    const userToken = wx.getStorageSync('token');  // 获取存储的 token
    const orderId = e.currentTarget.dataset.orderumber;
    wx.request({
      url: `http://localhost:8001/goods/goodsOrderApp/getOrderData/${orderId}`,
      header: {
        'Token': userToken,
      },
      success: (res) => {
        console.log(res);
        if (res.data.code === 20000) {
          this.setData({
            dialogAddMallGoods: true,
            tableData: res.data.data.tableData,
            fromData: {
              amount: res.data.data.amount,
              balance: res.data.data.balance
            },
            orderId: orderId
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error(err);
      }
    });
  },

  orderPay() {
    const userToken = wx.getStorageSync('token');
    wx.request({
      url: `http://localhost:8001/goods/goodsOrderApp/orderPay/${this.data.orderId}`,
      header: {
        'Token': userToken,
      },
      success: (res) => {
        // console.log(res);
        if (res.data.code === 20000) {
          this.setData({ dialogAddMallGoods: false });
          this.getOrders();
          wx.showToast({
            title: res.data.message,
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error(err);
      }
    });
  },

  orderComment(e) {
    const product = e.currentTarget.dataset.product;
    this.setData({
      dialogAddMallAs: true,
      comment: {
        mallGoodsId: product.goodsId,
        goodsUserId: wx.getStorageSync('userId') // 获取存储的用户ID
      }
    });
  },

  saveComment() {
    wx.request({
      url: 'http://localhost:8001/goods/commentGoods/saveOrUpdateCommentGoods',
      method: 'POST',
      data: this.data.comment,
      success: (res) => {
        if (res.data.code === 20000) {
          wx.showToast({ title: res.data.message, icon: 'success' });
          this.setData({ comment: {}, dialogAddMallAs: false });
        } else {
          wx.showToast({ title: res.data.message, icon: 'none' });
        }
      },
      fail: (err) => {
        console.error(err);
      }
    });
  },

  gbComment() {
    this.setData({
      comment: {},
      dialogAddMallAs: false
    });
  }
});
